package com.oms.app.food.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.app.food.model.Food;
import com.oms.app.food.service.IFoodService;
import com.oms.page.PageModel;

/**
 * 食品相关业务
 * controller
 */
@RequestMapping("")
public class FoodAction {
	
	private IFoodService foodService;
	
	public void setFoodService(IFoodService foodService) {
		this.foodService = foodService;
	}
	
	//绑定时间格式
	@InitBinder  
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, true));
    }
	
	// 进入食物管理页
	@RequestMapping(value="/food_foodManagerList/{shopid}")
	public String enterFoodManagerList(@PathVariable String shopid,HttpServletRequest request) {
		request.setAttribute("shopid",shopid);
		return "manager/food/foodManagerList";
	}
	
	// 获取食物列表
	@ResponseBody
	@RequestMapping(value="/food_getList/{shopid}")
	public Map<String, Object> getShopList(@PathVariable String shopid,HttpServletRequest request){
		request.setAttribute("shopid", shopid);
		List<Food> list =  foodService.getFoodList(request);
		return PageModel.getListToString(list, request);
	}

	// 增加食物基本信息
	@ResponseBody
	@RequestMapping(value="/food_addFood", method=RequestMethod.POST)
	public Map<String, Object> addFood(Food foodForm, HttpServletRequest request){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if (foodService.addFood(foodForm)) {
			jsonMap.put("success", true);
			jsonMap.put("msg", "增加食物成功！");
		} else {
			jsonMap.put("success", false);
			jsonMap.put("msg", "增加食物失败！");
		}
		return jsonMap;
	}
	
	// 修改食物基本信息
	@ResponseBody
	@RequestMapping(value="/food_updateFood/{foodId}", method=RequestMethod.POST)
	public Map<String, Object> updateFood(Food foodForm, @PathVariable String foodId, HttpServletRequest request){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		foodForm.setId(Integer.valueOf(foodId));
		if (foodService.updateFood(foodForm)) {
			jsonMap.put("success", true);
			jsonMap.put("msg", "修改食物成功！");
		} else {
			jsonMap.put("success", false);
			jsonMap.put("msg", "修改食物失败！");
		}
		return jsonMap;
	}
	
	// 删除食物信息
	@ResponseBody
	@RequestMapping(value="/food_deleteFood/{foodId}")
	public Map<String, Object> enterShopList(@PathVariable String foodId, HttpServletRequest request) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if (foodService.deleteFood(foodId)) {
			jsonMap.put("success", true);
			jsonMap.put("msg", "删除食物成功！");
		} else {
			jsonMap.put("success", true);
			jsonMap.put("msg", "删除食物成功！");
		}
		return jsonMap;
	}
}